﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
using Oracle.DataAccess.Client;
using System.Configuration;

namespace Testify
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // If a student is taking a test, redirect back to test
            foreach (string i in Session.Contents)
            {
                if (i == "isTakingTest")
                    if ((bool)Session["isTakingTest"] == true)
                        Response.Redirect("~/Tests/TakeTest.aspx");
            }

            #region Show different panels according to the User Type
            string uType = string.Empty;
            string query = @"select u.user_type_id
                           from user_type u join testify_user t on u.user_type_id = t.user_type_id
                           where u.user_type_id = t.user_type_id and t.user_id = :user_id";

            OracleConnection connection = new OracleConnection(ConfigurationManager.ConnectionStrings["Singularity"].ConnectionString);
            OracleCommand oCmd = new OracleCommand(query, connection);
            OracleDataReader reader = default(OracleDataReader);

            try
            {
                connection.Open();

                oCmd.Parameters.Clear();
                oCmd.Parameters.Add("user_id", Session["UserID"]);

                reader = oCmd.ExecuteReader();
                while (reader.Read())
                {
                    uType = reader.GetValue(0).ToString();
                }
                reader.Close();

                Label lbl = new Label();
                switch (uType)
                {
                    case "0":
                        Page.Title = "Control Panel";
                        lbl = this.Master.FindControl("lblPageTitle") as Label;
                        lbl.Text = "Control Panel";
                        break;
                    case "1":
                        Page.Title = "Master Control Desk";
                        lbl = this.Master.FindControl("lblPageTitle") as Label;
                        lbl.Text = "Master Control Desk";
                        this.pnl_adminView.Visible = true;
                        break;
                    case "2":
                        Page.Title = "Lectern";
                        lbl = this.Master.FindControl("lblPageTitle") as Label;
                        lbl.Text = "Lectern";
                        this.pnl_teacherView.Visible = true;
                        break;
                    case "3":
                        Page.Title = "Desk";
                        lbl = this.Master.FindControl("lblPageTitle") as Label;
                        lbl.Text = "Desk";
                        this.pnl_studentView.Visible = true;
                        break;
                }
            }
            catch (Exception)
            {
                // Debugging
                 //Response.Write("<h1>" + ex + "</h1>");
            }
            finally
            {
                oCmd.Dispose();
                connection.Close();
            }
            #endregion
        }

        protected void giveTest(object sender, EventArgs e)
        {
            LinkButton lb = new LinkButton();
            TimeSpan ts = new TimeSpan();

            Int64 start   =  1;
            Int64 current =  0;
            Int64 end     = -1;

            lb = sender as LinkButton;
            Session["takeTest_id"] = lb.CommandArgument;
            Session["assign_id"] = lb.CommandName;

            #region Get the assignment start and end
            string query = @"select TO_CHAR(assignment_start, 'YYYYMMDDHH24MISS'), TO_CHAR(sysdate, 'YYYYMMDDHH24MISS'), TO_CHAR(assignment_end, 'YYYYMMDDHH24MISS')
                             from assignment
                             where test_id = :p_TestID";

            OracleConnection connection = new OracleConnection(ConfigurationManager.ConnectionStrings["Singularity"].ConnectionString);
            OracleCommand oCmd0 = new OracleCommand(query, connection);
            OracleDataReader reader = default(OracleDataReader);

            try
            {
                connection.Open();

                oCmd0.Parameters.Clear();
                oCmd0.Parameters.Add("test_id", lb.CommandArgument);

                reader = oCmd0.ExecuteReader();
                reader.Read();
                start   = Convert.ToInt64(reader.GetValue(0).ToString());
                current = Convert.ToInt64(reader.GetValue(1).ToString());
                end     = Convert.ToInt64(reader.GetValue(2).ToString());
                reader.Close();
            }
            catch (Exception)
            {
                //Response.Write("<h1>" + ex + "</h1>");
            }
            finally
            {
                oCmd0.Dispose();
                connection.Close();
            }
            #endregion

            if (current <= end && current >= start)
            {

                query = @"select TO_CHAR(assignment_limit, 'HH24:MI')
                    from assignment
                    where test_id = :p_TestID";

                OracleCommand oCmd = new OracleCommand(query, connection);

                try
                {
                    connection.Open();

                    oCmd.Parameters.Clear();
                    oCmd.Parameters.Add("test_id", lb.CommandArgument);

                    reader = oCmd.ExecuteReader();
                    reader.Read();
                    ts = new TimeSpan(int.Parse(reader.GetValue(0).ToString().Substring(0, 2)), int.Parse(reader.GetValue(0).ToString().Substring(3)), 0);
                    reader.Close();
                }
                catch (Exception)
                {
                    //Response.Write("<h1>" + ex + "</h1>");
                }
                finally
                {
                    oCmd.Dispose();
                    connection.Close();
                }

                Session["endTest"] = DateTime.Now.Add(ts);
                Session["isTakingTest"] = true;

                Response.Redirect("~/Tests/TakeTest.aspx");
            }
            else
            {
                lblWaitError.Visible = true;
            }
        }

        protected void ddlCurrentTerm_DataBound(object sender, EventArgs e)
        {
            OracleCommand cmdGetCurrentTerm = new OracleCommand(
                "SELECT current_term_id FROM environment",
                new OracleConnection(ConfigurationManager.ConnectionStrings["Singularity"].ConnectionString));

            cmdGetCurrentTerm.Connection.Open();
            ddlCurrentTerm.SelectedValue = cmdGetCurrentTerm.ExecuteScalar().ToString();
            cmdGetCurrentTerm.Connection.Close();
        }

        protected void btnCurrentTerm_Click(object sender, EventArgs e)
        {
            dsCurrentTerm.Update();
            ddlCurrentTerm.DataBind();
        }
    }
}
